from model.blog_model import Blog
from model.admin_and_blog import Admin_Link_Blog
from database_connection.index import Session
from model.class_and_blog import Class_Link_Blog
from model.class_model import Class
from utils.utils import add_model
from sqlalchemy import insert, delete

session = Session()


def add_one_blog(blog_info, class_id_list):
    try:
        blog = add_model(blog_info, "blog")
        session.add(blog)
        session.commit()
        class_list = session.query(Class).filter(Class.id.in_(class_id_list)).all()
        for item in class_list:
            blog.blog_class_list.append(item)
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        session.commit()
        return {"msg": "success", "data": blog}


def find_one_blog(blog_info):
    try:
        data = session.query(Blog).filter_by(**blog_info).first()
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
    else:
        return {"msg": "success", "data": data}


def pagination_query(str_part, page_number, page_size):
    # page_size每页显示的条数
    # page_number当前是第几页
    # 计算偏移量
    offset = (page_number - 1) * page_size
    try:
        if str_part == "":
            blog_list = session.query(Blog).limit(page_size).offset(offset).all()
        else:
            blog_list = session.query(Blog).filter(Blog.label.like(f"%{str_part}%")).limit(page_size).offset(offset).all()
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        return {"msg": "success", "data": blog_list}


def find_count():
    try:
        count = session.query(Blog).count()
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        return {"msg": "success", "data": count}


def find_is_like(admin_id, blog_ids):
    try:
        data = session.query(Admin_Link_Blog).filter(
            Admin_Link_Blog.admin_id == admin_id,
            Admin_Link_Blog.blog_id.in_(blog_ids)
        )
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        return {"msg": "success", "data": data}


def find_like_count(str_part):
    try:
        count = session.query(Blog).filter(Blog.label.like(f"%{str_part}%")).count()
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        return {"msg": "success", "data": count}


def find_class_related_blog(class_id, limit, offset):
    try:
        data = session.query(Blog).join(Class_Link_Blog).filter(Class_Link_Blog.class_id == class_id).limit(limit).offset(offset).all()
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        return {"msg": "success", "data": data}


def drop_like(admin_id, blog_id):
    try:
        session.execute(
            insert(Admin_Link_Blog).values(admin_id=admin_id, blog_id=blog_id)
        )
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        session.commit()
        return {"msg": "success", "data": None}


def del_drop_like(admin_id, blog_id):
    try:
        session.execute(
            delete(Admin_Link_Blog).where(
                (Admin_Link_Blog.admin_id == admin_id) &
                (Admin_Link_Blog.blog_id == blog_id)
            )
        )
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        session.commit()
        return {"msg": "success", "data": None}


def update_like_number(blog_id, instruction):
    try:
        subquery = session.query(Blog.like_number).filter(Blog.id == blog_id).subquery()
        if instruction == "+":
            stmt = session.query(Blog).filter(Blog.id == blog_id).update({Blog.like_number: subquery.as_scalar() + 1})
        elif instruction == "-":
            stmt = session.query(Blog).filter(Blog.id == blog_id).update({Blog.like_number: subquery.as_scalar() - 1})
    except Exception as e:
        session.rollback()
        return {"msg": "error"}
        # 将错误信息返回给前端
    else:
        session.commit()
        return {"msg": "success", "data": None}


